﻿/*
 * MemoryFile.h
 *
 */
#ifndef __GDK_MEMORYFILE_H__
#define __GDK_MEMORYFILE_H__


#include <gdk/core/Types.h>
#include <gdk/core/Archive.h>
#include <gdk/core/String.h>
#include <gdk/core/MemoryBlock.h>




namespace gdk
{



class GDK_CORE_EXPORT MemoryFile : public Archive
{
	DECLARE_CLASS_HANDLE(MemoryFile)
	DECLARE_CLASS_INSTANCE(MemoryFile)

	enum BlockSize {
		MEMORY_BLOCK_SIZE = 4096,
	};
	enum CacheCount {
		MEMORY_CACHE_COUNT = (1024 * 1024) / MEMORY_BLOCK_SIZE,
	};

private:
	class Cache : public MemoryBlock<MEMORY_BLOCK_SIZE>, public Unknown
	{
		DECLARE_CLASS_HANDLE(Cache)
		DECLARE_CLASS_INSTANCE(Cache)

	private:
		Cache(void)
		{}
	};


private:
	typedef Cache::Handle					Cache_Handle;
	typedef std::map<ssize_t, Cache_Handle>	CacheMap;
	typedef CacheMap::iterator				CacheMap_Iterator;
	typedef CacheMap::const_iterator		CacheMap_ConstIterator;


	CacheMap mutable	_mapCache;
	ssize_t				_dataSize;
	ssize_t				_curPosition;


protected:
	MemoryFile(void);
	~MemoryFile(void);

public:
	virtual ssize_t getArchiveSize(void) const;
	virtual ssize_t getPosition(void) const;
	virtual ssize_t setPosition(ssize_t pos);

public:
	virtual ssize_t available(void) const;
	virtual ssize_t peek(void *buf, ssize_t size) const;
	virtual ssize_t read(void *buf, ssize_t size);
	virtual ssize_t write(const void *buf, ssize_t size);
};





}//namespace gdk



#endif /*__GDK_MEMORYFILE_H__*/
